<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="预警级别" prop="alarmLevel">
        <el-input
          v-model="queryParams.alarmLevel"
          placeholder="请输入预警级别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测站名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入测站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置名称" prop="locationId">
        <el-select v-model="queryParams.locationId" placeholder="请选择上/下行" style="width: 100%;">
          <el-option
            v-for="tunnel in tunnelAll"
            :key="tunnel.id"
            :label="tunnel.tunnelName"
            :value="tunnel.id"
          >
            <span style="float: left">{{ tunnel.tunnelName }}（{{ tunnel.direction }}）</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择预警类型" clearable size="small">
          <el-option
            v-for="dict in equFaultAlarmTypes"
            :key="dict.diceValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="来报时间" prop="reportTime">
          <el-date-picker
            v-model="queryParams.reportTime"
            clearable
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择来报时间"
          />
        </el-form-item> -->
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option label="待处理" value="0" />
          <el-option label="不予处理" value="1" />
          <el-option label="已处理" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content-container height_100-query_from">
      <div class="handle_bar">
        <div class="tit"><span>设备列表</span></div>
        <div>
          <el-radio-group v-model="queryParams.equType" size="small" style="margin-right:20px;" @change="handleQuery">
            <el-radio-button v-for="equ in equAlarmType" :key="equ.equType" :label="equ.equType">{{ equ.equTypeName }}（{{ equ.num }}）</el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <div class="flex-1" style="position: relative;overflow: hidden;">
        <div style="position: absolute;left: 0;top: 0;bottom: 0;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="loading"
            class="flex-1"
            height="250"
            style="width:100%;"
            :data="alarmList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column label="预警级别" align="center" prop="alarmLevel" />
            <el-table-column label="测站名称" align="center" prop="stationName" />
            <el-table-column label="安装位置" align="center" prop="locationName" />
            <el-table-column label="预警类型" align="center" prop="alarmType" :formatter="alarmTypeformat" />
            <el-table-column label="预警值(mm)" align="center" prop="alarmValue" />
            <el-table-column label="时段值(mm)" align="center" prop="periodValue" />
            <el-table-column label="超限(mm)" align="center" prop="transfinite" />
            <el-table-column label="来报时间" align="center" prop="reportTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="处理状态" align="center" prop="status">
              <template slot-scope="scope">
                <span v-if="scope.row.status == 0" class="text-danger">待处理</span>
                <span v-if="scope.row.status == 1">不予处理</span>
                <span v-if="scope.row.status == 2" class="text-navy">已处理</span>
              </template>
            </el-table-column>
            <!-- <el-table-column label="设备id" align="center" prop="equId" /> -->
            <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <!-- <el-button
                v-hasPermi="['lc:alarm:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                v-hasPermi="['lc:alarm:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button> -->
                <template v-if="scope.row.status == 0">
                  <el-popconfirm
                    title="是否确认处理？"
                    placement="left-end"
                    @confirm="handleHandle(scope.row)"
                  >
                    <!--  @click="handleHandle(scope.row)" -->
                    <el-button
                      slot="reference"
                      size="mini"
                      type="text"
                    >处理</el-button>
                  </el-popconfirm>
                  <el-divider direction="vertical" />
                  <el-popconfirm
                    title="是否确认操作？"
                    placement="left-end"
                    @confirm="handleNohandle(scope.row)"
                  >
                    <!-- @click="handleNohandle(scope.row)" -->
                    <el-button
                      slot="reference"
                      size="mini"
                      type="text"
                    >不予处理</el-button>
                  </el-popconfirm>
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改设备故障预警对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="预警级别" prop="alarmLevel">
              <el-input v-model="form.alarmLevel" placeholder="请输入预警级别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="测站名称" prop="stationName">
              <el-input v-model="form.stationName" placeholder="请输入测站名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装位置" prop="locationName">
              <el-input v-model="form.locationName" placeholder="请输入位置名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警类型" prop="alarmType">
              <el-input v-model="form.alarmType" placeholder="请选择预警类型" />
              <!-- <el-select v-model="form.alarmType" placeholder="请选择预警类型">
                  <el-option label="请选择字典生成" value="" />
                </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警值" prop="alarmValue">
              <!-- <el-input v-model="form.alarmValue" placeholder="请输入预警值" /> -->
              <el-input-number v-model="form.alarmValue" label="请输入预警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时段值" prop="periodValue">
              <!-- <el-input v-model="form.periodValue" placeholder="请输入时段值" /> -->
              <el-input-number v-model="form.periodValue" label="请输入时段值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="超限" prop="transfinite">
              <!-- <el-input v-model="form.transfinite" placeholder="请输入超限" /> -->
              <el-input-number v-model="form.transfinite" label="请输入超限" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来报时间" prop="reportTime">
              <el-date-picker
                v-model="form.reportTime"
                clearable
                size="small"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择来报时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态">
              <el-radio-group v-model="form.status">
                <el-radio label="0">待处理</el-radio>
                <el-radio label="1">不予处理</el-radio>
                <el-radio label="2">已处理</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警设备">
              <el-radio-group v-model="form.equType">
                <el-radio label="1">风机预警</el-radio>
                <el-radio label="2">照明预警</el-radio>
                <el-radio label="3">消防水泵预警</el-radio>
                <el-radio label="4">UPS电力预警</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- <el-form-item label="设备id" prop="equId">
            <el-input v-model="form.equId" placeholder="请输入设备id" />
          </el-form-item> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAlarm, getAlarm, delAlarm, addAlarm, updateAlarm, getAlarmType } from '@/api/fjgs/warning/monitor/deviceFault.js'
import { listAll as getTunnelAll } from '@/api/system/tunnel'
export default {
  name: 'Alarm',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备故障预警表格数据
      alarmList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmLevel: null,
        stationName: null,
        locationName: null,
        alarmType: null,
        alarmValue: null,
        periodValue: null,
        transfinite: null,
        reportTime: null,
        status: null,
        equId: null,
        equType: '1'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      radio3: null,
      tunnelAll: [],
      equFaultAlarmTypes: [],
      equAlarmType: []
    }
  },
  created() {
    this.getList()
    getTunnelAll().then(res => {
      this.tunnelAll = res.data
    })
    this.getDicts('equ_fault_alarm').then(response => {
      this.equFaultAlarmTypes = response.data
    })
    getAlarmType().then(res => {
      this.equAlarmType = res.data
    })
  },
  methods: {
    alarmTypeformat(row) {
      return this.selectDictLabel(this.equFaultAlarmTypes, row.alarmType)
    },
    handleHandle(row) {
      const _this = this
      const id = row.id || this.ids
      getAlarm(id).then(response => {
        const from = response.data
        // this.$confirm('是否确认处理?', '提示', {
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // }).then(function() {
        from.status = 2
        updateAlarm(from).then(response => {
          _this.msgSuccess('修改成功')
          _this.getList()
        })
        // }).catch(() => {})
      })
    },
    handleNohandle(row) {
      const _this = this
      const id = row.id || this.ids
      getAlarm(id).then(response => {
        const from = response.data
        // this.$confirm('是否确认操作?', '提示', {
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // }).then(function() {
        from.status = 1
        updateAlarm(from).then(response => {
          _this.msgSuccess('修改成功')
          _this.getList()
        })
        // }).catch(() => {})
      })
    },
    /** 查询设备故障预警列表 */
    getList() {
      this.loading = true
      listAlarm(this.queryParams).then(response => {
        this.alarmList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        alarmLevel: null,
        stationName: null,
        locationName: null,
        alarmType: null,
        alarmValue: null,
        periodValue: null,
        transfinite: null,
        reportTime: null,
        status: 0,
        equId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加设备故障预警'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAlarm(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改设备故障预警'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAlarm(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAlarm(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除设备故障预警编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delAlarm(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lc/alarm/export', {
        ...this.queryParams
      }, `lc_alarm.xlsx`)
    }
  }
}
</script>

